package com.sunday.ble.orlant;

import android.app.Application;


import com.sunday.ble.orlant.greendao.DaoMaster;
import com.sunday.ble.orlant.greendao.DaoSession;
import com.sunday.ble.orlant.greendao.Device;

import java.util.List;

/**
 * Created by wang on 2016/4/25.
 */
public class BaseApp extends Application implements Thread.UncaughtExceptionHandler{


    private static BaseApp instance;
    public DaoSession daoSession;

    public DaoMaster daoMaster;

    public List<Device> devices;

    public synchronized static BaseApp getInstance() {
        return instance;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Thread.setDefaultUncaughtExceptionHandler(this);
        devices = getDaoSession().getDeviceDao().loadAll();
    }
    public DaoMaster getDaoMaster(){
        if (daoMaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"bluetooth.db", null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }
    public DaoSession getDaoSession(){
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        ex.printStackTrace();
    }
}
